#include "myQGraphics.h"

MyQGraphicsView::MyQGraphicsView()
{

}

void MyQGraphicsView::keyPressEvent(QKeyEvent *event)
{

    MyQGraphicsScene* myScene = (MyQGraphicsScene*)this->scene();
    qDebug()<<"鼠标事件,当前几层楼"<<Data::currentFloor;
    QPalette palette;
    //Data::canUserKey = false;
    if(!Data::canUserKey){
        switch(event->key()){
        case Qt::Key_Up :
            //label1 = new QLabel("请选择一个：\n"+shopMsgList.at(0)+"\n"+shopMsgList.at(1)+"\n"+shopMsgList.at(2)+"\n"+shopMsgList.at(3));
            myScene->shopMsgList[myScene->choose[3]].replace("▶", "▷");
            myScene->choose[3]--;
            myScene->choose[3]=myScene->choose[3]<0?3:myScene->choose[3]--;

            myScene->shopMsgList[myScene->choose[3]].replace("▷","▶");
            myScene->label1->setText("请选择一个：\n"+myScene->shopMsgList.at(0)+"\n"+myScene->shopMsgList.at(1)+"\n"+myScene->shopMsgList.at(2)+"\n"+myScene->shopMsgList.at(3));
            break;
        case Qt::Key_Down :
            myScene->shopMsgList[myScene->choose[3]].replace("▶", "▷");
            myScene->choose[3]++;
            myScene->choose[3]=myScene->choose[3]>3?0:myScene->choose[3]++;

            myScene->shopMsgList[myScene->choose[3]].replace("▷","▶");
            myScene->label1->setText("请选择一个：\n"+myScene->shopMsgList.at(0)+"\n"+myScene->shopMsgList.at(1)+"\n"+myScene->shopMsgList.at(2)+"\n"+myScene->shopMsgList.at(3));
            break;
        case Qt::Key_Left :
            if (myScene->player->getPosX() - 1 < 11 && myScene->player->getPosX() - 1 >= 0) {
                myScene->player->setToward(0);
                myScene->interaction(myScene->player->getPosX()-1, myScene->player->getPosY());
                myScene->draw(0);
            };
            break;
        case Qt::Key_Right :
            if (myScene->player->getPosX() + 1 < 11 && myScene->player->getPosX() + 1 >= 0) {
                myScene->player->setToward(2);
                myScene->interaction(myScene->player->getPosX()+1, myScene->player->getPosY());
                myScene->draw(0);
            };
            break;
        case Qt::Key_Space:
        case Qt::Key_Enter:
            qDebug()<<"加属性";
            myScene->addProp();
            break;
        case Qt::Key_K :
            palette.setBrush(QPalette::Background,QBrush(myScene->blankBgImg));// 使用平滑的缩放方式
            myScene->qWidget->setPalette(palette);// 给widget加上背景图
            if(myScene->qWidget->isHidden()){
                myScene->qWidget->show();
            }else {
                myScene->qWidget->hide();

            }
            break;
        case Qt::Key_1 :

           myScene->showInfo("测试信息提示");
            break;

        }

        return;
    }


    switch(event->key()){
    //0-左 1-下 2-右 3-上
    case Qt::Key_Up :
        if (myScene->player->getPosY() - 1 < 11 && myScene->player->getPosY() - 1 >= 0) {
            myScene->player->setToward(3);
            myScene->interaction(myScene->player->getPosX(), myScene->player->getPosY() - 1);
            myScene->draw(0);
        };
        break;
    case Qt::Key_Down :
        if (myScene->player->getPosY() + 1 < 11 && myScene->player->getPosY() + 1 >= 0) {
            myScene->player->setToward(1);
            myScene->interaction(myScene->player->getPosX(), myScene->player->getPosY() + 1);
            myScene->draw(0);
        };
        break;
    case Qt::Key_Left :
        if (myScene->player->getPosX() - 1 < 11 && myScene->player->getPosX() - 1 >= 0) {
            myScene->player->setToward(0);
            myScene->interaction(myScene->player->getPosX()-1, myScene->player->getPosY());
            myScene->draw(0);
        };
        break;
    case Qt::Key_Right :
        if (myScene->player->getPosX() + 1 < 11 && myScene->player->getPosX() + 1 >= 0) {
            myScene->player->setToward(2);
            myScene->interaction(myScene->player->getPosX()+1, myScene->player->getPosY());
            myScene->draw(0);
        };
        break;
    case Qt::Key_K :

        myScene->qWidget->show();
        break;

    }

    //    if(event->key() == Qt::Key_Up){
    //        Data::currentFloor++;
    //        if(Data::currentFloor>Data::maxFloor) {
    //            Data::currentFloor = Data::maxFloor;
    //            return;
    //        }
    //    }

    //    if(event->key() == Qt::Key_Left){
    //        myScene->showInfo("得到一把钥匙");
    //        return;
    //    }

    //    if(event->key() == Qt::Key_Down){
    //        Data::currentFloor--;
    //        if(Data::currentFloor<Data::minFloor) {
    //            Data::currentFloor = Data::minFloor;
    //            return;
    //        }
    //        if(myScene != NULL || myScene != nullptr)
    //            myScene->draw(0);
    //    }

}



















MyQGraphicsScene::MyQGraphicsScene()
{
    //绘制背景
    choose[0] = 0;
    choose[1] = 0;
    choose[2] = 0;
    choose[3] = 0;
    data = new Data();
    //memcpy(initPos1,data->initPos,sizeof(data->initPos));
    memcpy(map1,data->map,sizeof(data->map));
    player = new Player();
    gameBgImg.load("img/GameBg.png");
    blankBgImg.load("img/BlankBg.png");
    battleBgImg.load("img/BattleBg.png");

    gameItem = new QGraphicsPixmapItem();

    gameItem->setPixmap(gameBgImg);

    QFont ft("Serif");
    ft.setPointSize(25);

    this->setFont(ft);
    this->addItem(gameItem);

    font = QFont("Arial",20);

    this->setFont(font);



    battleScene = new QGraphicsScene();
    battleItem = new QGraphicsPixmapItem();
    battleItem->setPixmap(battleBgImg);

    battleView = new QGraphicsView();
    battleView->setScene(this->battleScene);

    timer = new QTimer(this->battleScene);
    battleView->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint); // 去掉标题栏,去掉任务栏显示，窗口置顶

    //    QRect rect = battleView->geometry();
    //    battleView->setGeometry(rect.x(),rect.y()+200,rect.width(),rect.height());
    //connect(timer,SIGNAL(start()),this,SLOT(startBattle()));

    connect(timer,SIGNAL(timeout()),this,SLOT(startBattle()));

    qWidget = new QWidget();


    QFont font = QFont("Arial",20);
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::white);

    shopEvent(1);



    label1 = new QLabel("请选择一个：\n"+shopMsgList.at(0)+"\n"+shopMsgList.at(1)+"\n"+shopMsgList.at(2)+"\n"+shopMsgList.at(3));


    label1->setFont(font);
    label1->setPalette(pa);

    qWidget->resize(450,300);


    QLabel* shop = new QLabel();
    shop->setPixmap(*data->imagesMap0[22]);

    QGridLayout* mainLayout = new QGridLayout();
    QGridLayout* leftLayout = new QGridLayout();
    leftLayout->addWidget(shop);
    QVBoxLayout* right = new QVBoxLayout();
    right->addWidget(label1);
    mainLayout->addLayout(leftLayout,0,0,3,1);
    mainLayout->addLayout(right,0,1,7,4);
    qWidget->setLayout(mainLayout);

    QPalette palette2;
    palette2.setBrush(QPalette::Background,QBrush(blankBgImg));// 使用平滑的缩放方式
    qWidget->setPalette(palette2);// 给widget加上背景图
    qWidget->setObjectName("qWidget");
    qWidget->setStyleSheet("#qWidget{border:10px solid rgb(204, 102, 0);}");
    //qWidget->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    //qWidget->setWindowFlags(Qt::Widget | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowStaysOnTopHint);
    qWidget->raise();


}



MyQGraphicsScene::~MyQGraphicsScene()
{
}


void MyQGraphicsScene::addProp()
{
    switch (choose[3]) {
    case 0:
        player->setHp(player->getHp()+8);
        break;
    case 1:
        player->setAttack(player->getAttack()+4);
        break;
    case 2:
        player->setDefend(player->getDefend()+4);

        break;
    case 3:
        qWidget->hide();
        Data::canUserKey = true;
        break;
    }
}

QString MyQGraphicsScene::forecast(const Monster *e)
{
    if (player->getAttack() <= e->getDefend()) {
        return QString("???");
    } else if (player->getDefend() >= e->getAttack()) {
        return QString("0");
    } else {

        return QString::number(((e->getHp() / (player->getAttack() - e->getDefend())) * (e->getAttack() - player->getDefend())));
    }
}

void MyQGraphicsScene::shopEvent(const int id)
{
    QStringList list;
    QPalette palette2;

    switch (id) {
    case 0:     // 第 3 层商店
        list.insert(0,"▶增加 800 点生命（25 金币）");
        list.insert(1,"▷增加 4 点攻击（25 金币）");
        list.insert(2,"▷增加 4 点防御（25 金币）");
        list.insert(3,"▷离开商店");

        palette2.setBrush(QPalette::Background,*data->imagesMap0[22]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图

        break;
    case 1:     // 第 5 层 神秘老人

        list.insert(0,"▶提升一级（需要 100 点）");
        list.insert(1,"▷增加攻击5（需要 30 点） ");
        list.insert(2,"▷增加防御5（需要 30 点）");
        list.insert(3,"▷离开商店");
        palette2.setBrush(QPalette::Background,*data->imagesMap0[26]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图

        break;

    case 2:     // 第 5 层 商人

        list.insert(0,"▶购买 1 把黄钥匙（$ 10）");
        list.insert(1,"▷购买 1 把蓝钥匙（$ 50）");
        list.insert(2,"▷购买 1 把红钥匙（$ 100）");
        list.insert(3,"▷离开商店");
        palette2.setBrush(QPalette::Background,*data->imagesMap0[27]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图

        break;
    case 3:     // 第 11 层 商店
        list.insert(0,"▶增加 4000 点生命（100 金币）");
        list.insert(1,"▷增加 20 点攻击（100 金币）");
        list.insert(2,"▷增加 20 点防御（100 金币）");
        list.insert(3,"▷离开商店");
        palette2.setBrush(QPalette::Background,*data->imagesMap0[22]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图

        break;
    case 4:     // 第 12 层 商人
        list.insert(0,"▶卖出 1 把黄钥匙（$ 7）");
        list.insert(1,"▷卖出 1 把蓝钥匙（$ 35）");
        list.insert(2,"▷卖出 1 把黄钥匙（$ 70）");
        list.insert(3,"▷离开商店");
        palette2.setBrush(QPalette::Background,*data->imagesMap0[27]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图

        break;
    case 5:     // 第 13 层 神秘老人
        list.insert(0,"▶提升三级（需要 270 点）");
        list.insert(1,"▷增加攻击 17（需要 95 点）");
        list.insert(2,"▷增加防御 17（需要 95 点）");
        list.insert(3,"▷离开商店");
        palette2.setBrush(QPalette::Background,*data->imagesMap0[26]);// 使用平滑的缩放方式
        shopMsgList = list;
        qWidget->setPalette(palette2);// 给widget加上背景图
        break;
    }
}

void MyQGraphicsScene::draw(double sec)
{
    this->removeItem(gameItem);
    delete gameItem;

    gameItem = new QGraphicsPixmapItem();
    gameItem->setPixmap(gameBgImg);


    this->addItem(gameItem);
    qDebug()<<"重绘,几楼啊"<<Data::currentFloor;
    image++;
    qDebug()<<"data"<<data;
    if(image%2==0){
        // 绘制 地图数据
        for (int x = 0; x < 11; x++) {
            for (int y = 0; y < 11; y++) {
                int id = map1[Data::currentFloor][x][y];

                QPixmap* pix = data->imagesMap0[id];
                QGraphicsPixmapItem* pixItem = new QGraphicsPixmapItem();
                pixItem->setPixmap(*pix);
                pixItem->setPos(GAME_PIX_72 * y + GAME_PIX_72 * 6, GAME_PIX_72 * x + GAME_PIX_72);
                pixItem->setParentItem(gameItem);
                //scene->addItem(pixItem);
                //g2.drawImage(a, GAME_PIX_72 * y + GAME_PIX_72 * 6, GAME_PIX_72 * x + GAME_PIX_72, null);
            }
        }
    }else{
        // 绘制 地图数据
        for (int x = 0; x < 11; x++) {
            for (int y = 0; y < 11; y++) {
                int id = map1[Data::currentFloor][x][y];

                QPixmap* pix = data->imagesMap1[id];
                QGraphicsPixmapItem* pixItem = new QGraphicsPixmapItem();
                pixItem->setPixmap(*pix);
                pixItem->setPos(GAME_PIX_72 * y + GAME_PIX_72 * 6, GAME_PIX_72 * x + GAME_PIX_72);
                pixItem->setParentItem(gameItem);
                //scene->addItem(pixItem);
                //g2.drawImage(a, GAME_PIX_72 * y + GAME_PIX_72 * 6, GAME_PIX_72 * x + GAME_PIX_72, null);
            }
        }
    }

    //绘制玩家

    playerItem = new QGraphicsPixmapItem();
    playerItem->setPos((player->getPosX() + 6) * GAME_PIX_72, (player->getPosY() + 1) * GAME_PIX_72);

    QPixmap* playerPix = player->getPixMap();

    qDebug()<<"开始绘制玩家"<<playerPix;
    playerItem->setPixmap(*playerPix);

    qDebug()<<"开始绘制玩家";
    playerItem->setParentItem(gameItem);
    qDebug()<<"绘制玩家结束";
    //绘制左侧数据
    //g2.setFont(new Font("Arial", 0, 30));

    QGraphicsSimpleTextItem* level = new QGraphicsSimpleTextItem();
    level->setText(QString::number(player->getLevel()));
    level->setPos(220, 110);
    level->setFont(font);
    level->setBrush(Qt::white);
    level->setParentItem(gameItem);

    QGraphicsSimpleTextItem* hp = new QGraphicsSimpleTextItem();
    hp->setText(QString::number(player->getHp()));
    hp->setPos(220, 170);
    hp->setFont(font);
    hp->setBrush(Qt::white);
    hp->setParentItem(gameItem);


    QGraphicsSimpleTextItem* attack = new QGraphicsSimpleTextItem();
    attack->setText(QString::number(player->getAttack()));
    attack->setPos(220, 230);
    attack->setFont(font);
    attack->setBrush(Qt::white);
    attack->setParentItem(gameItem);


    QGraphicsSimpleTextItem* defend = new QGraphicsSimpleTextItem();
    defend->setText(QString::number(player->getDefend()));
    defend->setPos(220, 290);
    defend->setFont(font);
    defend->setBrush(Qt::white);
    defend->setParentItem(gameItem);


    QGraphicsSimpleTextItem* money = new QGraphicsSimpleTextItem();
    money->setText(QString::number(player->getMoney()));
    money->setPos(220, 350);
    money->setFont(font);
    money->setBrush(Qt::white);
    money->setParentItem(gameItem);

    QGraphicsSimpleTextItem* exp = new QGraphicsSimpleTextItem();
    exp->setText(QString::number(player->getExp()));
    exp->setPos(220, 410);
    exp->setFont(font);
    exp->setBrush(Qt::white);
    exp->setParentItem(gameItem);

    QGraphicsSimpleTextItem* yKey = new QGraphicsSimpleTextItem();
    yKey->setText(QString::number(player->getYkey()));
    yKey->setPos(220, 500);
    yKey->setFont(font);
    yKey->setBrush(Qt::white);
    yKey->setParentItem(gameItem);


    QGraphicsSimpleTextItem* bKey = new QGraphicsSimpleTextItem();
    bKey->setText(QString::number(player->getBkey()));
    bKey->setPos(220, 573);
    bKey->setFont(font);
    bKey->setBrush(Qt::white);
    bKey->setParentItem(gameItem);


    QGraphicsSimpleTextItem* rkey = new QGraphicsSimpleTextItem();
    rkey->setText(QString::number(player->getRkey()));
    rkey->setPos(220, 640);
    rkey->setFont(font);
    rkey->setBrush(Qt::white);
    rkey->setParentItem(gameItem);


    QGraphicsSimpleTextItem* floor = new QGraphicsSimpleTextItem();
    floor->setText(QString::number(Data::currentFloor));
    floor->setPos(200, 720);
    floor->setFont(font);
    floor->setBrush(Qt::white);
    floor->setParentItem(gameItem);


    QGraphicsSimpleTextItem* time = new QGraphicsSimpleTextItem();
    Data::gameSec+=sec;
    if (Data::gameSec == 60.0) {
        Data::gameSec = 0;
        Data::gameMin++;
    }

    time->setText("游戏时间："+QString::number(Data::gameMin)+"分"+QString::number(Data::gameSec));

    time->setPos(95, 830);
    time->setFont(QFont("Arial",14));
    time->setBrush(Qt::white);
    time->setParentItem(gameItem);


}

void MyQGraphicsScene::drawBattle()
{
    this->battleScene->removeItem(battleItem);
    delete battleItem;

    battleItem = new QGraphicsPixmapItem();
    battleItem->setPixmap(battleBgImg);

    this->battleScene->addItem(battleItem);

    QGraphicsPixmapItem* monsterItem =  new QGraphicsPixmapItem();
    qDebug()<<"怪物图片"<<monsteId;
    QPixmap* pixmap = data->imagesMap0[this->monsteId];
    qDebug()<<"怪物图片没了"<<pixmap;
    QPixmap monPix = pixmap->scaled(120, 120, Qt::KeepAspectRatio);
    monsterItem->setPixmap(monPix);
    monsterItem->setPos(85,120);
    monsterItem->setParentItem(battleItem);

    //双方减少血量计算
    int playerDHp =  monster.getAttack()-player->getDefend();
    int monDHp =  player->getAttack()-monster.getDefend();

    if(inBattle){
        player->setHp(player->getHp()-playerDHp);
        monster.setHp(monster.getHp()-monDHp);
    }

    QGraphicsSimpleTextItem* hp = new QGraphicsSimpleTextItem();
    QString hpStr = QString::number(player->getHp());
    hp->setText(hpStr);
    hp->setPos(755-(hpStr.length()-2)*20, 61);
    hp->setFont(font);
    hp->setBrush(Qt::white);
    hp->setParentItem(battleItem);


    QGraphicsSimpleTextItem* attack = new QGraphicsSimpleTextItem();
    attack->setText(QString::number(player->getAttack()));
    attack->setPos(755, 195);
    attack->setFont(font);
    attack->setBrush(Qt::white);
    attack->setParentItem(battleItem);


    QGraphicsSimpleTextItem* defend = new QGraphicsSimpleTextItem();
    defend->setText(QString::number(player->getDefend()));
    defend->setPos(755, 325);
    defend->setFont(font);
    defend->setBrush(Qt::white);
    defend->setParentItem(battleItem);




    if(monster.getHp()<=0) {
        timer->stop();
        endBattle();
    }
    QGraphicsSimpleTextItem* mhp = new QGraphicsSimpleTextItem();
    mhp->setText(QString::number(monster.getHp()));
    mhp->setPos(420, 62);
    mhp->setFont(font);
    mhp->setBrush(Qt::white);
    mhp->setParentItem(battleItem);


    QGraphicsSimpleTextItem* mAttack = new QGraphicsSimpleTextItem();
    mAttack->setText(QString::number(monster.getAttack()));
    mAttack->setPos(420, 195);
    mAttack->setFont(font);
    mAttack->setBrush(Qt::white);
    mAttack->setParentItem(battleItem);


    QGraphicsSimpleTextItem* mDefend = new QGraphicsSimpleTextItem();
    mDefend->setText(QString::number(monster.getDefend()));
    mDefend->setPos(420, 320);
    mDefend->setFont(font);
    mDefend->setBrush(Qt::white);
    mDefend->setParentItem(battleItem);

}

void MyQGraphicsScene::showInfo(const QString &msg)
{

    blankBgImg.load("img/BlankBg.png");
    qLabel.setPixmap(blankBgImg);

    qLabel.setText(msg);
    qLabel.setFont(QFont("Arial",30));
    qLabel.setGeometry(5, 400, GAME_PIX_72 * 18 - 20, 150);
    qLabel.setStyleSheet("QLabel{border:5px solid rgb(204, 102, 0);}");
    qLabel.setWindowFlags(Qt::Widget | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowStaysOnTopHint);
    QTimer::singleShot(1000,&qLabel,SLOT(hide()));
    qLabel.raise();
    qLabel.show();

}



void MyQGraphicsScene::interaction(const int x, const int y)
{
    int id = map1[Data::currentFloor][y][x];

    switch (id) {
    case 0:     // 玩家移动
        player->move(x, y);
        break;
    case 1:     // 砖墙
        break;
    case 2:     // 黄门
        if (player->getYkey() > 0) {
            map1[Data::currentFloor][y][x] = 0;
            player->setYkey(player->getYkey() - 1);
            player->move(x, y);
        }
        break;
    case 3:     // 蓝门
        if (player->getBkey() > 0) {
            map1[Data::currentFloor][y][x] = 0;
            player->setBkey(player->getBkey() - 1);
            player->move(x, y);
        }
        break;
    case 4:     // 红门
        if (player->getRkey() > 0) {
            map1[Data::currentFloor][y][x] = 0;
            player->setRkey(player->getRkey() - 1);
            player->move(x, y);
        }
        break;
    case 5:     // 石块
        break;
    case 6:     // [道具] 黄钥匙
        map1[Data::currentFloor][y][x] = 0;
        player->setYkey(player->getYkey() + 1);
        showInfo("得到一把 黄钥匙 ！");
        break;
    case 7:     // [道具] 蓝钥匙
        map1[Data::currentFloor][y][x] = 0;
        player->setBkey(player->getBkey() + 1);
        showInfo("得到一把 蓝钥匙 ！");
        break;
    case 8:     // [道具] 红钥匙

        map1[Data::currentFloor][y][x] = 0;
        player->setRkey(player->getRkey() + 1);
        showInfo("得到一把 红钥匙 ！");
        break;
    case 9:     // [道具] 蓝宝石
        map1[Data::currentFloor][y][x] = 0;
        player->setDefend(player->getDefend() + 3);
        showInfo("得到一个蓝宝石 防御力加 3 ！");
        break;
    case 10:    // [道具] 红宝石
        map1[Data::currentFloor][y][x] = 0;
        player->setAttack(player->getAttack() + 3);
        showInfo("得到一个红宝石 攻击力加 3 ！");
        break;
    case 11:    // [道具] 红药水
        map1[Data::currentFloor][y][x] = 0;

        player->setHp(player->getHp() + 200);
        showInfo("得到一个小血瓶 生命加 200 ！");
        break;
    case 12:    // [道具] 蓝药水
        map1[Data::currentFloor][y][x] = 0;
        player->setHp(player->getHp() + 500);
        showInfo("得到一个大血瓶 生命加 500 ！");
        break;
    case 13:    // 上楼
        data->currentFloor++;
        player->move(Data::initPos[data->currentFloor][0], Data::initPos[data->currentFloor][1]);
        break;
    case 14:    // 下楼
        data->currentFloor--;
        player->move(Data::finpost[data->currentFloor][0], Data::finpost[data->currentFloor][1]);
        break;
    case 15:    // 不可通过的护栏
        break;
    case 19:    // 火海
        break;
    case 20:    // 星空
        break;
    case 22:    // 商店
        choose[0] = Data::currentFloor;
        choose[1] = x;
        choose[2] = y;
        choose[3] = 0;
        shopEvent(0);
        Data::canUserKey = false;
        this->qWidget->show();
        break;
    case 24:    // [对话] 仙子
        map1[Data::currentFloor][y][x] = 0;
        player->setBkey(player->getBkey()+1);
        player->setYkey(player->getYkey()+1);
        player->setRkey(player->getRkey()+1);
        showInfo("去吧，打败大魔王！");
        player->move(x, y);

        break;
    case 25:    // [对话] 小偷
    case 26:    // [对话] 老人
    case 27:    // [对话] 商人
        //                        case 28:    // [对话] 公主
        //                    map1[Data::currentFloor][y][x] = 0;
        //                    player.move(x, y);
        //                    break;
    case 30:    // [道具] 小飞羽
        map1[Data::currentFloor][y][x] = 0;
        player->setLevel(player->getLevel() + 1);
        player->setHp(player->getHp() + 1000);
        player->setAttack(player->getAttack() + 7);
        player->setDefend(player->getDefend() + 7);
        showInfo("得到 小飞羽 等级提升一级 ！");
        break;
    case 31:    // [道具] 大飞羽
        map1[Data::currentFloor][y][x] = 0;
        player->setLevel(player->getLevel() + 3);
        player->setHp(player->getHp() + 3000);
        player->setAttack(player->getAttack() + 21);
        player->setDefend(player->getDefend() + 21);
        showInfo("得到 大飞羽 等级提升三级 ！");
        break;
    case 32:    // [宝物] 幸运十字架
        map1[Data::currentFloor][y][x] = 0;
        Data::isHasCross = true;
        showInfo("【幸运十字架】 把它交给序章中的仙子，可以将自身的所有能力提升一些（攻击、防御和生命值）。");
        break;
    case 33:    // [宝物] 圣水瓶
        map1[Data::currentFloor][y][x] = 0;
        player->setHp(player->getHp() * 2);
        showInfo("【圣水瓶】 它可以将你的体质增加一倍（生命值加倍）。");
        break;
    case 34:    // [宝物] 圣光徽
        map1[Data::currentFloor][y][x] = 0;
        Data::isHasForecast = true;
        showInfo("【圣光徽】 按 L 键使用 查看怪物的基本情况。");
        break;
    case 35:    // [宝物] 风之罗盘
        map1[Data::currentFloor][y][x] = 0;
        Data::isHasJump = true;
        showInfo("【风之罗盘】 按 J 键使用 在已经走过的楼层间进行跳跃。");
        break;
    case 36:    // [道具] 钥匙盒
        map1[Data::currentFloor][y][x] = 0;
        player->setYkey(player->getYkey() + 1);
        player->setBkey(player->getBkey() + 1);
        player->setRkey(player->getRkey() + 1);
        showInfo("得到 钥匙盒 各种钥匙数加 1 ！");
        break;
    case 38:    // [宝物] 星光神榔
        map1[Data::currentFloor][y][x] = 0;
        Data::isHasHammer = true;
        showInfo("【星光神榔】 把它交给第四层的小偷，小偷便会用它打开第十八层的隐藏地面（你就可以救出公主了）。");
        break;
    case 39:    // [道具] 金块
        map1[Data::currentFloor][y][x] = 0;
        player->setMoney(player->getMoney() + 300);
        showInfo("得到 金块 金币数加 300 ！");
        break;
    case 40:    // [怪物 monster]
    case 41:    // [怪物 monster]
    case 42:    // [怪物 monster]
    case 43:    // [怪物 monster]
    case 44:    // [怪物 monster]
    case 45:    // [怪物 monster]
    case 46:    // [怪物 monster]
    case 47:    // [怪物 monster]
    case 48:    // [怪物 monster]
    case 49:    // [怪物 monster]
    case 50:    // [怪物 monster]
    case 51:    // [怪物 monster]
    case 52:    // [怪物 monster]
    case 53:    // [怪物 monster]
    case 54:    // [怪物 monster]
    case 55:    // [怪物 monster]
    case 56:    // [怪物 monster]
    case 57:    // [怪物 monster]
    case 58:    // [怪物 monster]
    case 59:    // [怪物 monster]
    case 60:    // [怪物 monster]
    case 61:    // [怪物 monster]
    case 62:    // [怪物 monster]
    case 63:    // [怪物 monster]
    case 64:    // [怪物 monster]
    case 65:    // [怪物 monster]
    case 66:    // [怪物 monster]
    case 67:    // [怪物 monster]
    case 68:    // [怪物 monster]
    case 69:    // [怪物 monster]
        qDebug()<<forecast(data->monsterMap[id]);
        if (forecast(data->monsterMap[id]) == QString("???")
                || forecast(data->monsterMap[id]).toInt()>= player->getHp()){
            return;
        } else {

            this->monsteId = id;
            qDebug()<<"怪物id"<<this->monsteId;

            Monster* mon = data->monsterMap[id];
            monster.setId(id);
            monster.setHp(mon->getHp());
            monster.setDefend(mon->getDefend());
            monster.setAttack(mon->getAttack());
            monster.setExp(mon->getExp());
            monster.setMoney(mon->getMoney());

            this->nextX = x;
            this->nextY = y;
            Data::canUserKey = false;
            drawBattle();
            this->battleView->show();
            qDebug()<<"准备战斗";
            timer->start(500);
        }
        break;
    case 71:    // [宝物] 铁剑
        map1[Data::currentFloor][y][x] = 0;
        player->setAttack(player->getAttack() + 10);
        showInfo("得到 铁剑 攻击加 10 ！");
        break;
    case 73:    // [宝物] 钢剑
        map1[Data::currentFloor][y][x] = 0;
        player->setAttack(player->getAttack() + 30);
        showInfo("得到 钢剑 攻击加 30 ！");
        break;
    case 75:    // [宝物] 圣光剑
        map1[Data::currentFloor][y][x] = 0;
        player->setAttack(player->getAttack() + 120);
        showInfo("得到 圣光剑 攻击加 120 ！");
        break;
    case 76:    // [宝物]
        map1[Data::currentFloor][y][x] = 0;
        player->setDefend(player->getDefend() + 10);
        showInfo("得到 铁盾 防御加 10 ！");
        break;
    case 78:    // [宝物] 钢盾
        map1[Data::currentFloor][y][x] = 0;
        player->setDefend(player->getDefend() + 30);
        showInfo("得到 钢盾 防御加 30 ！");
        break;
    case 80:    // [宝物] 星光盾
        map1[Data::currentFloor][y][x] = 0;
        player->setDefend(player->getDefend() + 120);
        showInfo("得到 星光盾 防御加 120 ！");
        break;
    case 115:   // 可通过的护栏
        map1[Data::currentFloor][y][x] = 0;
        player->move(x, y);
        break;
    case 119:   // 同 case 1:
    case 129:   // 同 case 1:
        map1[Data::currentFloor][y][x] = 0;
        player->move(x, y);
        break;
    case 188:
        if (forecast(data->monsterMap[id]) == QString("???")
                || forecast(data->monsterMap[id]).toInt()>= player->getHp()){
            return;
        } else {

            this->monsteId = id;
            qDebug()<<"怪物id"<<this->monsteId;
            this->nextX = x;
            this->nextY = y;
            Data::canUserKey = false;
            drawBattle();
            this->battleView->show();
            qDebug()<<"准备战斗";
            timer->start(500);
        }
        break;
    case 202:   // [宝物] 炎之灵杖
        showInfo("得到 炎之灵杖");
        map1[Data::currentFloor][y][x] = 0;
        break;
    case 203:   // [宝物] 心之灵杖
        showInfo("得到 心之灵杖");
        map1[Data::currentFloor][y][x] = 0;
        player->move(x, y);
        break;
    case 301:   // 22层 到 24层
        Data::currentFloor += 2;
        player->move(5, 1);
        break;
    case 302:   // 22层 到 25层
        Data::currentFloor += 3;
        player->move(1, 5);
        break;
    case 303:   // 24层 到 22层
        Data::currentFloor -= 2;
        player->move(5, 9);
        break;
    case 304:   // 25层 到 22层
        Data::currentFloor -= 3;
        player->move(9, 5);
        break;
    case 305:   // 24层 到 26层
        Data::currentFloor += 2;
        player->move(5, 10);
        break;
    }
}

void MyQGraphicsScene::startBattle()
{

    inBattle = true;
    Data::canUserKey = false;
    drawBattle();
}

void MyQGraphicsScene::endBattle()
{
    qDebug()<<"开始战斗";
    map1[Data::currentFloor][nextY][nextX] = 0;
    inBattle = true;
    Data::canUserKey = true;
    player->setExp(monster.getExp()+player->getExp());
    player->setMoney(monster.getMoney()+player->getMoney());
    battleView->hide();
}

